/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.brand;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * The table ol_brand_stat mapping model.
 * </p>
 */
@TableBind(tableName = "ol_brand_stat", pkName = "brand")
public class BrandStat extends Model<BrandStat> {

    private static final long   serialVersionUID = 1L;
    /**
     * The public dao.
     */
    public static final BrandStat dao = new BrandStat();


    public List<BrandStat> list(){
    	return find(SqlKit.sql("brandstat.list"));
    }

    public int follow(long brandid, long mid, int n) {


        BrandStat stat = findById(brandid);
        if (stat == null) {
            stat = new BrandStat();
            stat.set("brand", brandid);
            stat.set("stats", n);
            stat.save();
            return 1;
        } else {
            int cur = stat.getInt("stats");
            cur += n;
            stat.set("stats", cur);
            stat.update();
            return cur;
        }
    }
}